問題描述
我返回集合的 Asp.net mvc 操作生成包含 system.linq.Enumerable 的 url,如何刪除它們 (My Asp.net mvc actions that return collection generate urls containing system.linq.Enumerable, how to remove them)
I have an mvc application that basically generates views which are bascially collections but collections of a particular type..so i.e.
IEnumerable<IType>
Now my urls contain stuff like this
www.site/home/section/param?System.Linq.Enumerable%.....
I want to remove anything after section.
I have tried the routeMap but have been unable to ommit the system.linq etc. Any ideas or help please
Here is my action method
public ActionResult Whatever(IEnumerable<IType> whatever)
{
return View(whatever);
}
參考解法
方法 1:
You can't pass around collections like that in the URL, you shouldn't add them to the RouteValueDictionary
when you redirect. Just send some basic information that can help you get what you need in the action you are redirecting to.
Edit: Based on your code, we can see that your action method takes an IEnumerable
, so wherever you are calling this action, you're passing it one. You can't do that, you'll have to generate your list in the action method. Try something like:
public ActionResult Whatever()
{
List<IType> whatever = new List<IType>();
//populate your list here, then we can return it
return View(whatever);
}
方法 2:
The question above is a very interesting and took me a while and has go back to my previously written mvc app. The answer is simple but not obvious.
And it seems in the current mvc3 even if you pass the object as anonymous type mapping your view to your view model, it always works out the collectiontype and somehow appends that to the url in this case it was appending whole case to the url as
http://www.website.com/param=system.linq.enumerable.where.select..
any way the correct way is to wrap it in a routevaluedictionary
new RouteValueDictionary(new { controller = Constants.HOMECONTROLLER, action = Constants.APPLYAPP }));
and if you are passing any stuff from one action to another use this
return new RedirectToRouteResult(Constants.DEFAULTROUTE,
new RouteValueDictionary(new { controller = Constants.HOMECONTROLLER, action = Constants.APPLYAPP }));
instead of RedirecttoAction because it seems this results in the url above.
Thanks MattyTommo for the help but its irrelevant in my case but it may be helpful for others. I cannot mark mattytommo's reply as answer at all because it actually misunderstood my request, perhaps my request wasnt correct but also misguided me.
(by user182630、mattytommo、user182630)